﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ingebet.Model;
using Ingebet.Model.Domain;

using Ingebet.Model.Domain;

namespace Ingebet.Web
{
    public partial class PageRencontre : System.Web.UI.Page
    {
        private string competition;

        public string Competition
        {
            get
            {
                if (competition == null)
                {
                    competition = Request["compet"].ToString();
                }
                return competition;
            }
        }

        private string equipe1;

        public string Equipe1
        {
            get
            {
                if (equipe1 == null)
                {
                    equipe1 = Request["equipe1"].ToString();
                }
                return equipe1;
            }
        }

        private string equipe2;

        public string Equipe2
        {
            get
            {
                if (equipe2 == null)
                {
                    equipe2 = Request["equipe2"].ToString();
                }
                return equipe2;
            }
        }

        private DateTime? date;

        public DateTime Date
        {
            get
            {
                if (date == null)
                {
                    date = Convert.ToDateTime(Request["date"].ToString());
                }
                return date.Value;
            }
        }

        protected void Page_Load(object sender , EventArgs e)
        {
            if (!IsPostBack)
            {
                SetChamps();
            }
        }

        private void SetChamps()
        {
            var lstSport = Global.Container.Resolve<SportModel>().GetAll();
            this.ddlSport.DataSource = lstSport;
            this.ddlSport.DataBind();
            this.txtCompetition.Text = this.Competition;
            this.txtEquipe1.Text = this.Equipe1;
            this.txtEquipe2.Text = this.Equipe2;
            this.txtDate.Text = this.Date.ToShortDateString();
        }

        protected void btnEnregistrer_Click(object sender , EventArgs e)
        {
            int idSport = Convert.ToInt32(this.ddlSport.SelectedValue);
            var ren = new Rencontre()
            {
                SportMatch = Global.Container.Resolve<SportModel>().GetByID(idSport) ,
                CompetitionMatch = new Competition() { Nom = this.txtCompetition.Text } ,
                Equipe1 = new Equipe() { Nom = this.txtEquipe1.Text } ,
                Equipe2 = new Equipe() { Nom = this.txtEquipe2.Text } ,
                DateRencontre = Convert.ToDateTime(this.txtDate.Text) ,
                CoteMatch = new Cote()
                    {
                        CoteGain = (this.txtCoteGain.Text != string.Empty) ? float.Parse(this.txtCoteGain.Text) : default(float) ,
                        CoteEgalite = (this.txtCoteNul.Text != string.Empty) ? float.Parse(this.txtCoteNul.Text) : default(float) ,
                        CotePerte = (this.txtCotePerte.Text != string.Empty) ? float.Parse(this.txtCotePerte.Text) : default(float)
                    } ,
                Score = ""
            };
            Global.Container.Resolve<RencontreModel>().Insert(ren);
            this.ClientScript.RegisterStartupScript(this.GetType() , "" , "window.close();" , true);
        }

        protected void btnFermer_Click(object sender , EventArgs e)
        {
            this.ClientScript.RegisterStartupScript(this.GetType() , "" , "window.close();" , true);
        }
    }
}